From b20b0f6bd8ba024c4eba43fd2eee9b78615cd4e3 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 5 Feb 2015 23:28:18 -0800 Subject: [PATCH] Add git2-curl as a dependency Due to libgit2 not supporting HTTP proxies, the custom transport API of the library must be used to reimplement the HTTP transport with proxy support. The git2-curl crate implements this transport on top of the curl-rust crate. By using libcurl we gain all sorts of proxy support for free. This transport is not used by default, however, as it is not well battle tested and the architecture is not currently ideal (download the entire repo into memory on a clone). Only when an HTTP proxy is present is the new transport used. The other drawback of git2-curl is that it does not currently support authentication. If a private git repository is cloned or authentication is required then it will generate an error instead of correctly asking for credentials. Closes #636 --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/bin/cargo.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 32cbb704e..14dd8761b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,7 @@ dependencies = [ "env_logger 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "git2-curl 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "hamcrest 0.1.0 (git+https://github.com/carllerche/hamcrest-rust.git)", "log 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -100,6 +101,16 @@ dependencies = [ "url 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "git2-curl" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "curl 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", + "git2 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "url 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "glob" version = "0.1.9" diff --git a/Cargo.toml b/Cargo.toml index e691eddd0..2517dc562 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ curl = "0.1" tar = "0.1" flate2 = "0.1" git2 = "0.1" +git2-curl = "0.1" glob = "0.1" time = "0.1" log = "0.2" diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index cdaa43adf..7bf0a1161 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -1,5 +1,6 @@ #![feature(collections, core, io, path, env)] +extern crate "git2-curl" as git2_curl; extern crate "rustc-serialize" as rustc_serialize; extern crate cargo; extern crate env_logger; @@ -88,6 +89,8 @@ macro_rules! each_subcommand{ ($mac:ident) => ({ fn execute(flags: Flags, config: &Config) -> CliResult> { config.shell().set_verbose(flags.flag_verbose); + init_git_transports(config); + if flags.flag_list { println!("Installed Commands:"); for command in list_commands().into_iter() { @@ -247,3 +250,32 @@ fn list_command_directory() -> Vec { } dirs } + +fn init_git_transports(config: &Config) { + // Only use a custom transport if a proxy is configured, right now libgit2 + // doesn't support proxies and we have to use a custom transport in this + // case. The custom transport, however, is not as well battle-tested. + match cargo::ops::http_proxy(config) { + Ok(Some(..)) => {} + _ => return + } + + let handle = match cargo::ops::http_handle(config) { + Ok(handle) => handle, + Err(..) => return, + }; + + // The unsafety of the registration function derives from two aspects: + // + // 1. This call must be synchronized with all other registration calls as + // well as construction of new transports. + // 2. The argument is leaked. + // + // We're clear on point (1) because this is only called at the start of this + // binary (we know what the state of the world looks like) and we're mostly + // clear on point (2) because we'd only free it after everything is done + // anyway + unsafe { + git2_curl::register(handle); + } +} -- 2.30.2